package com.sikaryofficial.backend.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author : qinjinyuan
 * @desc : 执行时间打印
 * @date : 2023/12/23 14:35
 */
@Aspect
@Component
@Slf4j
public class LogAspectHandler {
    @Value("${hismk.backend.logAspect.costTime}")
    private Long costTime;
    private String methodName;      // 方法名
    private long startTime;         // 开始时间
    /**
     * 定义一个切面，拦截 com.mutest.controller 包和子包下的所有方法
     */
    @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
    public void aopPointCut() {}

    @Before("aopPointCut()")
    public void doBefore(JoinPoint joinPoint) {
        methodName = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        startTime =System.currentTimeMillis();
    }
    @After("aopPointCut()")
    public void doAfter() {
        long eTime = System.currentTimeMillis() - startTime;
        if(costTime <= eTime ){
            log.info("execute " + methodName + " cost time：" + eTime + "ms");
        }
    }
}
